#include "BinaryTree.h"

int BTSize(BTNode* root)
{
	/*if (root == NULL)
		return 0;
	
	return BTSize(root->left) + BTSize(root->right) + 1;*/

	return root == NULL ? 0 : BTSize(root->left) + BTSize(root->right) + 1;
}

void BTprevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}

	printf("%d ", root->data);
	BTprevOrder(root->left);
	BTprevOrder(root->right);
}

void BTinOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}

	BTinOrder(root->left);
	printf("%d ", root->data);
	BTinOrder(root->right);
}

void BTbackOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}

	BTbackOrder(root->left);
	BTbackOrder(root->right);
	printf("%d ", root->data);
}

int BTHeight(BTNode* root)
{
	if (root == NULL)
		return 0;

	int left_height = BTHeight(root->left);
	int right_height = BTHeight(root->right);

	return left_height > right_height ? left_height + 1 : right_height + 1;

	//return (int)fmax(BTHeight(root->left), BTHeight(root->right)) + 1;
}

int BTlevelSize(BTNode* root, int* pi)
{
	if (root == NULL)
		return 0;

	if (*pi == 1)
		return 1;

	(*pi)--;
	return BTlevelSize(root->left, pi) + BTlevelSize(root->right, pi);
}


